package cn.js.ccit.flowersmall.user.controller;

import cn.js.ccit.flowersmall.user.entity.UserEntity;
import cn.js.ccit.flowersmall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

//创建退出接口
@RestController
public class CustomController {
    @Autowired
    ConsumerTokenServices consumerTokenServices;
    @Autowired
    UserService userService;
//前端无法发送Cookie，所以暂时删除
    @DeleteMapping("/api/logout")
    public boolean logout(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies){
                if(cookie.getName().equals("Token"))
                {
                    //使用token服务销毁
                    return consumerTokenServices.revokeToken(cookie.getValue());
                }
            }
        }
       // return false;
        //前端cookie不能发送token
        return true;
    }
    @GetMapping("/permis")
    public Authentication getUser(Authentication authentication) {
        return authentication;
    }
    @GetMapping("/userInfo")
    public UserEntity findUserByName(String username){
        UserEntity user = userService.findUserByName(username);
        return user;
    }
}

